<?php

namespace App\Controller\Admin\Rbac;

use App\Controller\Admin\BaseController;
use App\Service\Rbac\RbacService;
use App\Utils\Helper;
use App\Validator\Admin\Rbac\RouteValidator;
use Hyperf\Di\Annotation\Inject;

class RouteController extends BaseController
{

    #[Inject]
    protected RouteValidator $routeValidator;

    #[Inject]
    protected RbacService $rbacService;

    public function pageList()
    {
    }

    public function detail()
    {
    }

    public function add()
    {
        $va = $this->routeValidator->add($this->allParams());
        [$r, $msg] = $this->rbacService->addRoute($va, $this->getAdminId());
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function update()
    {
        $va = $this->routeValidator->update($this->allParams());
        [$r, $msg] = $this->rbacService->updateRoute(intval($va['id']), $va);
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function del()
    {
        $va = $this->routeValidator->del($this->allParams());
        [$r, $msg] = $this->rbacService->delRoutes($va['ids']);
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function all()
    {
        $data = $this->rbacService->getAllRoutes();
        return $this->success([
            'lists' => $data,
            'tree' => Helper::generateTree($data),
        ]);
    }
}